package com.bookshop.controller;

import com.bookshop.entity.Book;
import com.bookshop.entity.BookList;
import com.bookshop.entity.ClassifyList;
import com.bookshop.entity.SimpleClassify;
import com.bookshop.entity.response.R;
import com.bookshop.entity.request.SearchForm;
import com.bookshop.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * ClassName: BookController
 * Package: com.bookshop.controller
 * Description:
 *
 * @Author BJX
 * @Create 2024/1/18 9:29
 * @Version 1.0
 */
@RestController
@RequestMapping("/book")
public class BookController {
    private BookService bookService;

    @Autowired
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    //图书详情
    @GetMapping("/detail")
    public R detailBook(@RequestParam Long id) {
        Book b = bookService.getBookById(id);

        if (b == null) {
            return R.error().message("查询图书失败!");
        }

        return R.ok().put("data", b);
    }

    //搜索页
    @GetMapping("/search")
    public R searchBook(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "id") String orderField,
            @RequestParam(defaultValue = "DESC") String orderStyle,
            @RequestParam(defaultValue = "0") int classifyId,
            @RequestParam(defaultValue = "0") int priceType,
            @RequestParam(defaultValue = "0") int pressId,
            @RequestParam String mess) {

        SearchForm searchForm = new SearchForm(pageNum, pageSize, orderField, orderStyle, classifyId, priceType, pressId, mess);

        BookList bookList = bookService.searchBook(searchForm);

        if (bookList.getBookCount() == 0) {
            return R.error().message("未查询到相关图书!");
        }

        return R.ok().put("data", bookList);
    }

    //首页图书分类展示
//    @GetMapping("/homepage")
//    public R homeClassify(@RequestParam(defaultValue = "1") int classifyId){
//
//        HashMap<String, List<Book>> hashMap = bookService.homeClassify(classifyId);
//
//        if(hashMap.size() == 0){
//            return R.error().message("加载失败!");
//        }
//
//        return R.ok().put("data",hashMap);
//    }


    // 首页图书分类展示
    @GetMapping("/homepage")
    public R homeClassify() {

        List<List<List<Book>>> bookList = bookService.homeClassify();

        if (bookList.size() == 0) {
            return R.error().message("加载失败!");
        }

        return R.ok().put("data", bookList);
    }

    //首页一级分类+二级分类
    @GetMapping("/getClassify")
    public R getClassfiy() {

        List<ClassifyList> classifyLists = bookService.getClassify();

        if (classifyLists.size() == 0) {
            return R.error().message("加载失败!");
        }

        return R.ok().put("data", classifyLists);
    }

    //搜索页筛选界面
    @GetMapping("/getSimpleClassify")
    public R getSimpleClassify() {

        List<List<SimpleClassify>> classifyLists = bookService.getSimpleClassify();

        if (classifyLists.size() == 0) {
            return R.error().message("加载失败!");
        }

        return R.ok().put("data", classifyLists);
    }


}
